/**
 * Copyright (c) 2020-2021 浦东大爷 All rights reserved.
 * 机会永远都是留给有准备的人
 */
package com.job.moudle3.task4;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author: gitwo
 * @date: 2020-11-22
 * @description:
 */
public class ManagerSystem {
    private List<Student> studentList;
    private Integer count;
    private static ManagerSystem instance;

    private ManagerSystem() {
        studentList = new ArrayList<>();
        count = 0;
    }

    /**
     * 增加学生的方法
     */
    private Boolean add(String name, Integer age) {
        String id = String.format("%06d", ++count);
        System.out.println(name + "的学号是" + id);
        studentList.add(new Student(id, name, age));
        return true;
    }

    /**
     * 删除学生的方法，输入学号
     */
    private Boolean del(int id) {
        if(id > studentList.size()) {
            System.out.println("学号不存在");
            return false;
        }
        System.out.println(studentList.get(id-1) + "删除成功");
        studentList.remove(id-1);
        return true;
    }
    /**
     * 查询学生信息(id为0则显示全部，否则查询单个学生信息)
     */
    private void show(int id) {
        if(studentList.size() == 0) {
            System.out.println("目前学生信息库为空");
            return;
        }
        if(id != 0 && id <= studentList.size()) {
            Student student = studentList.get(id-1);
            System.out.println("学号：" + student.getId() + "\t姓名：" + student.getName() + "\t\t年龄：" + student.getAge());
        } else if(id == 0){
            for(Student student: studentList) {
                System.out.println("学号：" + student.getId() + "\t姓名：" + student.getName() + "\t\t年龄：" + student.getAge());
            }
        } else {
            System.out.println("学号输入有误。");
        }
    }
    /**
     * 查询单个学生信息
     */
    private void get(int id) {
        show(id);
    }
    /**
     * 修改学生信息
     */
    private Boolean update(int id, String name, int age) {
        if(id > studentList.size()) {
            System.out.println("学号不存在");
            return false;
        }
        Student student = studentList.get(id-1);
        student.setAge(age);
        student.setName(name);
        studentList.remove(id-1);
        studentList.add(id-1, student);
        return true;
    }
    public void run() {
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎进入【拉勾教育- 大数据开发高薪训练营】学生管理系统");
        int iCount = 0;
        do {
            System.out.print("请输入用户名:");
            String username = sc.next();
            System.out.print("请输入密码:");
            String passwd = sc.next();
            if(!("admin".equals(username) && "123456".equals(passwd))) {
                System.out.println("用户名或密码错误，请重新输入！");
                iCount++;
            } else {
                System.out.println("登陆成功。");
                break;
            }
            if(iCount > 2) {
                System.out.println("系统登陆失败！请联系管理员");
                return;
            }
        }while (true);

        do {
            System.out.println("******[大数据开发高薪训练营]学生管理系统*******");
            System.out.println("*\t1.添加学生信息[输入add]\t\t\t\t\t*");
            System.out.println("*\t2.删除学生信息[输入del]\t\t\t\t\t*");
            System.out.println("*\t3.修改学生信息[输入update]\t\t\t\t*");
            System.out.println("*\t4.查询学生信息[输入get]\t\t\t\t\t*");
            System.out.println("*\t5.查询全部学生信息[输入show]\t\t\t\t*");
            System.out.println("*\t6.退出系统[输入quit]\t\t\t\t\t\t*");
            System.out.println("*********************************************");
            System.out.print("请输入你的选择:");
            String input = sc.next();
            switch (input){
                case "add":
                    System.out.print("请输入学生的姓名和年龄:");
                    String name = sc.next();
                    Integer age = sc.nextInt();
                    add(name, age);
                    break;
                case "del":
                    System.out.print("请输入学生的学号:");
                    if(sc.hasNextInt()) del(sc.nextInt());
                    else System.out.println("学号输入有误. >>" + sc.next());
                    break;
                case "update":
                    System.out.print("请输入学生的学号:");
                    int id = 0;
                    if(sc.hasNextInt()) {
                        id = sc.nextInt();
                        get(id);
                    }
                    else System.out.println("学号输入有误. >>" + sc.next());
                    System.out.print("请输入学生的姓名和年龄:");
                    String name2 = sc.next();
                    Integer age2 = sc.nextInt();
                    update(id, name2, age2);
                    break;
                case "get":
                    System.out.print("请输入学生的学号:");
                    if(sc.hasNextInt()) get(sc.nextInt());
                    else System.out.println("学号输入有误. >>" + sc.next());
                    break;
                case "show":
                    show(0);
                    break;
                case "quit":
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("选择无效，请重新选择");
                    break;
            }
        }while(true);
    }
    /**
     * 使用单例类对外提供服务
     * @return 单例类的对象
     */
    public static synchronized ManagerSystem getInstance() {
        if(instance == null) {
            instance = new ManagerSystem(); // 懒汉式加载方式，有延迟加载的优势
        }
        return instance;
    }

}
